package wangwenjun.phase2.concurrencypattern.activeobject.staticproxy;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import wangwenjun.phase2.concurrencypattern.activeobject.staticproxy.framework.OrderServiceFactory;
import wangwenjun.phase2.concurrencypattern.future.Future;

/**
 * @author ChangLiang
 * @date 2020/6/17
 */
@Slf4j
public class AppTest {

    @Test
    @DisplayName("test OrderServiceImpl use--createOrder")
    public void test() {
        OrderService orderService = new OrderServiceImpl();
        // class OrderServiceImpl
        System.out.println(orderService.getClass().toString());
        // main thread wait method invoked finished
        orderService.createOrder("changliang-account", 123456);
    }

    @Test
    @DisplayName("test OrderServiceImpl use--findOrderDetails")
    public void test3() throws InterruptedException {
        OrderService orderService = new OrderServiceImpl();
        // main thread wait method invoked finished
        Future<String> orderDetails = orderService.findOrderDetails(123456);
        // waiting
        String result = orderDetails.get();
        log.info("result:{}", result);
    }

    @Test
    @DisplayName("test OrderServiceImpl use--findOrderDetails")
    public void test4() throws InterruptedException {
        OrderService orderService = new OrderServiceImpl();
        // 仍然有一个线程去处理 但是主线程直接执行结束了
        // 只要不get 就不会阻塞main线程
        // 类似于目前app-user发短信的处理
        Future<String> orderDetails = orderService.findOrderDetails(123456);
        log.info("return immediately");
        Thread.currentThread().join();
    }

    @Test
    @DisplayName("test-activeObject--createOrder")
    public void test2() throws InterruptedException {
        // 与test不同的是 创建了一个OrderService的activeObject
        OrderService orderService = OrderServiceFactory.createActiveObject(new OrderServiceImpl());
        orderService.createOrder("changliang-account", 123456);
        log.info("return immediately");
        Thread.currentThread().join();
    }

    @Test
    @DisplayName("test active object--findOrderDetails")
    public void test5() throws InterruptedException {
        OrderService orderService = OrderServiceFactory.createActiveObject(new OrderServiceImpl());
        // 这里有一个FutureImpl#MUTEX
        Future<String> orderDetails = orderService.findOrderDetails(123456);
        // 这里挂起的是main thread on OrderServiceProxy#FutureImpl#MUTEX
        String result = orderDetails.get();
        log.info("result:{}", result);
    }

    @Test
    @DisplayName("test active object--findOrderDetails")
    public void test6() throws InterruptedException {
        OrderService orderService = OrderServiceFactory.createActiveObject(new OrderServiceImpl());
        // 仍然有一个线程去处理 但是主线程直接执行结束了
        // 只要不get 就不会阻塞main线程
        // 类似于目前app-user发短信的处理
        Future<String> orderDetails = orderService.findOrderDetails(123456);
        log.info("return immediately");
        Thread.currentThread().join();
    }

}
